#include <iostream>
using namespace std;

int sum(int data[][4], int sz) {
  int total = 0;
  for (int i = 0; i < sz; i++) {
    for (int j = 0; j < 4; j++) {
      total += data[i][j];
    }
  }
  return total;
}
int m1() {
  int data[3][4] = {{1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6}};
  int total = sum(data, 3); //这里的3表示有3行

  cout << total << endl;

  return 0;
}

//使用一个函数计算特定的字符在字符串中出现的次数
int c_in_str(const char *str, char ch) {
  int cnt = 0;
  while (*str) {
    if (*str == ch) {
      cnt++;
    }
    str++;
  }
  return cnt;
}
int m2() {
  char nums[15] = "minimum";
  // char *wail = "utulate";

  int ms = c_in_str(nums, 'm');
  // int ws = c_in_str(wail, 'u');

  cout << ms << endl;
  // cout << ws << endl;

  return 0;
}

//写一个函数返回字符串，要求打印某个字符多少次
char *buildstr(char c, int n) {
  char *ret = new char[n + 1];
  ret[n] = '\0';
  while (n--) {
    ret[n] = c;
  }
  return ret;
}
int m3() {
  cout << "请输入您想打印的字符";
  char ch;
  cin >> ch;
  cout << "请输入您想打印的次数";
  int times = 0;
  cin >> times;

  char *ret = buildstr(ch, times);
  cout << ret << endl;
  delete[] ret;
  ret = buildstr('+', 20);
  cout << ret << "-DONE-" << ret << endl;
  delete[] ret;

  return 0;
}

//传递和返回结构
struct travel_time {
  int hours;
  int mins;
};
travel_time sum(travel_time t1, travel_time t2) {
  travel_time total;
  total.mins = (t1.mins + t2.mins) % 60;
  total.hours = t1.hours + t2.hours + (t1.mins + t2.mins) / 60;
  return total; //返回的就是一个结构体
}
void show_times(travel_time ret) {
  cout << ret.hours << "-" << ret.mins << endl;
}
int m4() {
  travel_time day1 = {5, 45};
  travel_time day2 = {4, 55};

  travel_time ret = sum(day1, day2);
  cout << "2天总共花费了：" << endl;
  show_times(ret);

  travel_time day3 = {4, 32};
  cout << "3天总共花费了：" << endl;
  show_times(sum(day3, ret));

  return 0;
}

// 7.6.2
#include <math.h>
struct rect {
  double x;
  double y;
};
struct polar {
  double dis;   //表示距离
  double angle; //表示角度
};
void rect_to_polar(const rect *ret1, polar *ret2) {
  // polar answer;
  // answer.dis = sqrt((xytop.x * xytop.x) + (xytop.y * xytop.y));
  // answer.angle = atan2(xytop.y, xytop.x);
  // return answer;
  ret2->dis = sqrt((ret1->x * ret1->x) + (ret1->y * ret1->y));
  ret2->angle = atan2(ret1->y, ret1->x);
}
void show_polar(polar *ret) {
  const double pre = 57.29577951;
  cout << "dis:" << ret->dis << endl;
  cout << "angle:" << ret->angle * pre << endl;
}

int m5() {
  rect rplace;
  polar pplace;
  cout << "请输入x和y的值" << endl;
  while (cin >> rplace.x >> rplace.y) {
    // pplace = rect_to_polar(rplace);
    rect_to_polar(&rplace, &pplace);
    show_polar(&pplace);
    cout << "Next two numbers (q to quit):";
  }
  cout << "Done,\n";

  return 0;
}

// 7.14
#include <string>
void display(const string str[], int n) {
  for (int i = 0; i < n; i++) {
    cout << i + 1 << ":" << str[i] << endl;
  }
}
int m6() {
  string list[5]; // 5个字符串
  for (int i = 0; i < 5; i++) {
    cout << i + 1 << ": ";
    getline(cin, list[i]);
  }
  cout << "your list:" << endl;
  display(list, 5);

  return 0;
}

// 7.8.1递归
void countdown(int n) {
  cout << "Counting down...." << n << "(n at " << &n << ")" << endl;
  if (n > 0) {
    countdown(n - 1);
  }
  cout << n << ":Kavoom!"
       << "(n at " << &n << ")" << endl;
}
int m7() {
  countdown(4);

  return 0;
}

// 7.17函数指针
double betsy(int ret) { return 0.05 * ret; }
double pam(int ret) { return 0.03 * ret + 0.0004 * ret * ret; }
void estimate(int ret, double (*pf)(int)) { //函数作为参数
  cout << ret << "line will take";
  cout << (*pf)(ret) << " hours\n";
}
int main() {
  int code;
  cin >> code;
  cout << "Here's Betsy estimate:" << endl;
  estimate(code, betsy);

  cout << "Here's Pam estimate:" << endl;
  estimate(code, pam);

  return 0;
}
